

// Ajax请求
const Ajax = {
	get: function (url, fn) {
		// XMLHttpRequest对象用于在后台与服务器交换数据   
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, true);
		xhr.setRequestHeader("token",window.localStorage.getItem('TOken'));
		xhr.onreadystatechange = function () {
			// readyState == 4说明请求已完成
			if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
				// 从服务器获得数据 
				fn.call(this, xhr.responseText);
			}
		};
		xhr.send();
	},
	// datat应为'a=a1&b=b1'这种字符串格式，在jq里如果data为对象会自动将对象转成这种字符串格式
	post: function (url, data, fn) {
		var xhr = new XMLHttpRequest();
		xhr.open("POST", url, true);
		// 添加http头，发送信息至服务器时内容编码类型
		// console.log(window.localStorage.getItem('TOken'))
		xhr.setRequestHeader("Content-Type","application/json");
		xhr.setRequestHeader("token",window.localStorage.getItem('TOken'));
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
				fn.call(this, xhr.responseText);
			}
		};
		// xhr.send(data);
		xhr.send(JSON.stringify(data)); // Ensure data is JSON stringified
	}
}
// 时间戳
const getLocalTime = {
	getLocalTime:function(timestamp){
		if (timestamp.length == 10) {
			var date = new Date(timestamp * 1000);
		} else {
			var date = new Date(timestamp);
		}
		
		Y = date.getFullYear() + '-';
		M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
		h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
		m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
		s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
		return Y + M + D + h + m + s;
	}
}
